package com.example.demo.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.JavaType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class JsonUtil
{
    private static ObjectMapper mapper = null;

    static
    {
        mapper = new ObjectMapper();
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    }

    public static String toJson(Object o)
    {
        try
        {
            return mapper.writeValueAsString(o);
        }
        catch (Exception e)
        {
            log.error(e.getMessage());
            return "";
        }
    }

    public static <T> Object parseObj(String json, Class<T> targetClazz)
    {
        if (json.startsWith("["))
        {
            return fromListJson(json, targetClazz);
        }
        else
        {
            return fromJson(json, targetClazz);
        }
    }

    public static <T> T fromJson(String json, Class<T> targetClazz)
    {
        try
        {
            return mapper.readValue(json, targetClazz);
        }
        catch (Exception e)
        {
            log.error(e.getMessage()+";json:"+json);
            return null;
        }
    }

    /**
     * 解析json为泛型类  将json解析为T<V>类型
     * @param json
     * @param t
     * @param v
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T, V> T fromJson(String json, Class<T> t, Class<V> v) {
        JavaType javaType = mapper.getTypeFactory().constructParametricType(t, v);
        try {
            return mapper.readValue(json, javaType);
        } catch (IOException e) {
            log.error(e.getMessage()+";json:"+json);
            return null;
        }
    }

    public static <T> List<T> fromListJson(String json, Class<T> targetClazz)
    {
        List<T> ll = new ArrayList<T>();
        if (json == null || "".equals(json))
        {
            return ll;
        }

        try {
            Gson gson = new Gson();
            com.google.gson.JsonParser jsonParser = new com.google.gson.JsonParser();
            JsonElement jsonElement = jsonParser.parse(json);  //将json字符串转换成JsonElement
            JsonArray jsonArray = jsonElement.getAsJsonArray();  //将JsonElement转换成JsonArray
            Iterator<JsonElement> it = jsonArray.iterator();  //Iterator处理
            while (it.hasNext())
            {  //循环
                jsonElement = it.next(); //提取JsonElement
                json = jsonElement.toString();  //JsonElement转换成String
                T t = gson.fromJson(json, targetClazz); //String转化成JavaBean
                ll.add(t);  //加入List
            }
        } catch (Exception e) {
            log.error(e.getMessage()+";list json:"+json);
        }
        return ll;
    }

}
